package com.linkvin.ec.hcs.controller;

import com.google.common.collect.Lists;
import com.linkvin.ec.hcs.dao.model.mdm.VBrand;
import com.linkvin.ec.hcs.dao.model.mdm.bo.VModelBo;
import com.linkvin.ec.hcs.service.mdm.VBrandService;
import com.linkvin.ec.hcs.vo.RestVO;
import com.linkvin.ec.hcs.vo.VModelTreeVO;
import com.linkvin.ec.hcs.vo.VModelVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/brand", produces = MediaType.APPLICATION_JSON_VALUE)
public class VBrandController {


    @Autowired
    private VBrandService vBrandService;

    private static final Logger LOGGER = LoggerFactory.getLogger(VBrandController.class);

    @GetMapping(value = "/tree")
    public RestVO<List<VModelTreeVO>> getBrandTree(){

        RestVO<List<VModelTreeVO>> result = new RestVO<>(0,"success");

        LOGGER.info("VModelController.getBrandTree ");
        try{
            List<VModelTreeVO> vModelTreeVOList = new ArrayList<>();
            List<VBrand> vBrands = vBrandService.listAllBrand();
            vBrands.forEach(vBrand -> {
                VModelTreeVO vModelTreeVO = new VModelTreeVO();
                vModelTreeVO.setId(vBrand.getId());
                vModelTreeVO.setLabel(vBrand.getBrandName());
                vModelTreeVO.setMode("brand");
                vModelTreeVO.setIsleaf(false);
                vModelTreeVOList.add(vModelTreeVO);
            });
            result.setData(vModelTreeVOList);
        }catch (Exception ex){
            LOGGER.info("VModelController.getBrandTree ERROR Message : {} ", ex.getMessage());
            result.setCode(30005);
            result.setMessage("车型数据读取失败");
        }

        return result;
    }

    @GetMapping(value = "/all")
    public RestVO<List<VModelVO>> getAllBrand(){
        RestVO<List<VModelVO>> result = new RestVO<>(0,"success");
        LOGGER.info("VBrandController.getAllBrand params ");
        List<VBrand> vBrands = vBrandService.listAllBrand();
        List<VModelVO> listVModelVo = Lists.newArrayList();
        vBrands.forEach(vBrand -> {
            VModelVO vModelVO = new VModelVO();
            vModelVO.setBrandId(vBrand.getId());
            vModelVO.setBrandName(vBrand.getBrandName());
            listVModelVo.add(vModelVO);
        });
        result.setData(listVModelVo);
        return result;
    }


    @PostMapping(value = "/create")
    public RestVO<VModelVO> create(@RequestBody VModelBo vModelBo){
        RestVO<VModelVO> result = new RestVO<>(0,"success");
        LOGGER.info("VBrandController.create params : {}", vModelBo);
        try {

            VModelVO vModelVO =new VModelVO();
            vModelBo = vBrandService.addBrand(vModelBo);
            BeanUtils.copyProperties(vModelBo,vModelVO);
            result.setData(vModelVO);
        }catch (Exception e){
            LOGGER.error("VBrandController.create ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping(value = "/update")
    public RestVO<VModelVO> update(@RequestBody VModelBo vModelBo){
        RestVO<VModelVO> result = new RestVO<>(0,"success");
        LOGGER.info("VBrandController.update params : {}", vModelBo);
        try {

            VModelVO vModelVO =new VModelVO();
            vModelBo = vBrandService.updateBrand(vModelBo);
            BeanUtils.copyProperties(vModelBo,vModelVO);
            result.setData(vModelVO);
        }catch (Exception e){
            LOGGER.error("VBrandController.update ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/delete")
    public RestVO<Boolean> delete(@RequestBody List<Integer> ids){

        RestVO<Boolean> result = new RestVO<>(0,"success");
        LOGGER.info("VBrandController.delete params : {}", ids);

        try {
            int i =  vBrandService.deleteBrand(ids);
            Boolean blnrtn = i > 0;
            result.setData(blnrtn);
        }catch (Exception e){
            LOGGER.error("VBrandController.delete ERROR Message : {} ", e.getMessage());
            result.setData(false);
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

}
